package com.example.dao.articleMgmt;

import com.example.entity.ArticleEntity;
import com.example.entity.query.ArticlePagingQueryFilter;
import com.example.entity.viewModel.BatchUpdateArticleStatusRequest;
import com.example.entity.viewModel.UpdateArticleStatusRequest;
import com.example.common.base.PagingQueryResult;
import com.github.pagehelper.PageHelper;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by James.H.Fu on 2016/12/17.
 */
@Component
public class ArticleDao {
    @Autowired
    private SqlSession sqlSession;

    public int insert(ArticleEntity article) {
        return sqlSession.insert("insert", article);
    }

    public int batchInsert(List<ArticleEntity> articleList) {
        Map<String, Object> params = new HashMap<>();
        params.put("list", articleList);
        return sqlSession.insert("batchInsert", params);
    }

    public int update(ArticleEntity article) {
        return sqlSession.update("update", article);
    }

    public int batchUpdate(List<ArticleEntity> articleList) {
        Map<String, Object> params = new HashMap<>();
        params.put("list", articleList);
        return sqlSession.update("batchUpdate", params);
    }

    public int updateStatus(UpdateArticleStatusRequest req) {
        return sqlSession.update("updateStatus", req);
    }

    public int batchUpdateStatus(BatchUpdateArticleStatusRequest req) {
        return sqlSession.update("batchUpdateStatus", req);
    }

    public int delete(int id) {
        return sqlSession.delete("delete", id);
    }

    public int batchDelete(Object[] idArray) {
        return sqlSession.delete("batchDeleteByArray", idArray);
    }

    public int batchDelete(List<Integer> idList) {
        return sqlSession.delete("batchDeleteByList", idList);
    }

    public ArticleEntity load(int id) {
        return sqlSession.selectOne("load", id);
    }

    public List<ArticleEntity> queryAll() {
        return sqlSession.selectList("queryAll");
    }

    /**
     * 分页查询
     * 注意#{},${}的区别
     *
     * @param filter 查询参数
     * @return 分页结果
     */
    public PagingQueryResult<ArticleEntity> pagingQuery(ArticlePagingQueryFilter filter) {

        PageHelper.startPage(filter.getPageNumber(), filter.getPageSize());
        List<ArticleEntity> data = sqlSession.selectList("pagingQuery", filter);

        PagingQueryResult<ArticleEntity> result = new PagingQueryResult<ArticleEntity>();
        result.setPageResult(data);

        return result;
    }

    /**
     * executeScalar: 只获取第一行第一列的值
     * 1.获取一个Integer
     * 2.获取一个Date（日期的格式如何处理？）
     * 3.获取一个String(CHAR类型的字段如何trim?)
     */
    public Integer loadMaxArticleID() {
        Integer scalarValue = sqlSession.selectOne("loadMaxArticleID");

        return scalarValue;
    }

    public Date loadMaxPublishDate() {
        Date scalarValue = sqlSession.selectOne("loadMaxPublishDate");

        return scalarValue;
    }

    public String loadFirstTitle() {
        String scalarValue = sqlSession.selectOne("loadFirstTitle");

        return scalarValue;
    }

    /**
     * executeListInt:获取一个List<Integer>
     */

    public List<Integer> loadArticleIDList() {
        return sqlSession.selectList("loadArticleIDList");
    }

    /**
     * 调用存储过程,获取输入，输出参数及返回值
     */
    //http://stackoverflow.com/questions/13988935/return-value-mybatis
    //http://stackoverflow.com/questions/13988935/return-value-mybatis
}
